use std::collections::{HashMap, VecDeque};

#[allow(dead_code)]
pub fn test18() {
    let str = String::from("aaabbbccaaddaaaaddaa");
    let s1 = str.replace("aa", "77"); // 默认从头到尾
    let s2 = str.replacen("aa", "77", 2); // 替换指定的次数
    println!("s1={s1}");
    println!("s2={s2}");

    let mut  str = "hello".to_string();
    str.truncate(3); //保留指定长度的字符串
    println!("str={str}");
    // 清空字符串
    str.clear();
    println!("str={str}");

    let str = String::from("Löwe 老虎");
    let bytes = str.as_bytes();
    for b in bytes {
        print!("{} | ", b);
    }
    println!();
    for c in str.chars() {
        print!("{} | ", c);
    }
}
#[allow(dead_code)]
pub fn test17() {
    let s1 = String::from("h1");
    let s2 = String::from("h2");
    let s3 = "h3";
    // 注意加号的右边需要是字符串字面量
    // let str = s1 + s2.as_str() + s3 + "h4";
    // println!("{}", str);

    let str = format!("{}, {}, {}, {}", s1, s2, s3, "h4");
    println!("{}", str);
}

#[allow(dead_code)]
pub fn test16() {
    let mut str = String::from("Hello, ");
    str.push('R');
    println!("{}", str);
    str.push_str("ust!");
    println!("{}", str);
}
#[allow(dead_code)]
#[allow(unused_variables)]
pub fn test15() {
    // &str 是字符的集合，不可变的UTF-8编码字符串
    // 创建字符串字面量
    // 方式1
    let s1 = "hello world";
    let s1 = String::from("hello world");
    // 方式2
    let s2 = s1.as_str(); // 将字符串对象转换为字符串字面量

    // 创建字符串对象
    // 方式1
    let str = String::from("hello world");
    // 方式2
    let str = String::new();
    // 方式3 to_string() 方法
    let s3 = "hello world".to_string();
}

#[allow(dead_code)]
pub fn test14() {

    //哈希表
    let mut map = HashMap::new();
    map.insert("name", "rust");
    map.insert("age", "18");
    println!("{:?}", map);

    let mut map = HashMap::with_capacity(10);
    let r = map.insert("name", "rust1.8");
    println!("{:?}", r); // key: name 不存在，返回 None
    let r = map.insert("name", "rust1.9"); // 修改已经存在的值
    println!("{:?}", r); // key: name 存在，返回 Some(对应的值)
    println!("{:?}", map);

    // 不存在时写入 age=20
    let r = map.entry("age").or_insert("22");
    println!("{:?}", r);
    println!("{:?}", map);

    map.remove("name");
    println!("name={:?}", map.get("name")); // None
    println!("age={:?}", map.get("age")); // None
    for (k, v) in map {
        println!("key={},val={}", k, v);
    }
}

#[allow(dead_code)]
pub fn test13() {

    // 双端队列
    let mut stack = VecDeque::new();
    stack.push_front(1);
    stack.push_front(2);
    stack.push_back(3);
    stack.push_back(4);


    // stack.remove(10);
    // stack.pop_back();
    // stack.pop_front();
    println!("{:?}", stack);
}

#[allow(dead_code)]
pub fn test12() {
    let mut v1 = Vec::new();
    v1.push(1);
    v1.push(2);
    println!("{:?}, len={}, v[1]={}", v1, v1.len(), v1[1]);

    v1[0] = 100;
    v1.extend([4, 5, 6]);
    for i in &v1 {
        println!("{i}");
    }
    println!();
    // macro 宏
    let mut v2 = vec![1, 2, 3];
    v2.push(4);
    let v3 = Vec::from([1, 2, 3, 4]);
    println!("v2==v3 {}", v2 == v3);
    // assert_eq!(v2, vec![1, 2, 3, 4]);

    let mut v = Vec::with_capacity(3);
    v.resize(3, 2);
    println!("{:?}, len={}, v[0]={}", v, v[0], v[0]);

    let mut stack = Vec::new();
    stack.push(1);
    stack.push(2);
    stack.push(3);
    // println!("{:?}, len={}, v[0]={}", stack, v[10], v[0]);
    if let Some(r) = stack.get(2) {
        println!("r={}", r);
    } else {
        println!("超出索引");
    }

    // assert_eq!(100usize.saturating_sub(27), 73);
    // assert_eq!(13usize.saturating_sub(127), 0);

    // 计算需要的索引
    let index = stack.len() - 3;

    // let index = stack.len().saturating_sub(2);
    if let Some(v) = stack.get_mut(index) {
        *v *= 2;
    }
    while let Some(top) = stack.pop() {
        println!("{top}");
    }
    // 元素都已移除
    println!("stack len = {}", stack.len());

    // 长度为3，每个元素都是10
    let mut v = vec![10; 3];
    println!("{:?}, len={}, v[0]={}", v, v.len(), v[0]);
    // 移除元素
    v.remove(0);
    v.remove(0);
    v[0] = 100;
    // v.remove(0);
    // v.get(下标) 有值返回Some(值),无值返回None
    println!("{:?}, len={}, v[0]={}, v.get={:?}", v, v.len(), v[0], v.get(10));

    // 每个元素加50
    let mut v = vec![1, 2, 3];
    // 必须使用 &mut 才可以修改里面的元素
    for i in &mut v {
        *i += 50;
    }
    print!("{:?}", v);
}

#[allow(dead_code)]
#[derive(Debug)]
enum ColorNoParam {
    Red,
    Yellow,
    Blue,
}

#[allow(dead_code)]
#[derive(Debug)]
enum ColorParam {
    Red(String),
    Yellow(String),
    Blue(String),
}

#[allow(dead_code)]
pub fn test11() {
    let blue = ColorParam::Blue(String::from("blue"));
    println!("{:?}", blue);

    let color = ColorNoParam::Yellow;
    match color {
        ColorNoParam::Red => {
            println!("{:?}", ColorNoParam::Red);
        }
        ColorNoParam::Yellow => {
            println!("{:?}", ColorNoParam::Yellow);
        }
        _ => {
            println!("not supported");
        }
    }
    println!("{:?}", color)
}


// 元组结构体，只有类型，没有名称
#[allow(dead_code)]
#[derive(Debug)]
struct Color(i32, i32, i32);

#[allow(dead_code)]
struct Student {
    name: &'static str,
    score: i8,
}
#[allow(dead_code)]
pub fn test10() {
    let score = 59;
    let username = "张三";

    // 必须是 mut 修饰结构体才可以修改结构体的字段
    let mut stu = Student {
        score, // 同名的可以直接写字段名称
        name: username,
    };

    // 修改分数
    stu.score = 77;
    println!("name={}, score={}", stu.name, stu.score);

    let stu2 = Student {
        // name: "rust",
        score: 8,
        ..stu
    };
    println!("name={}, score={}", stu2.name, stu2.score);

    let color = Color(255, 255, 255);
    println!("{:?}", color);
}

#[allow(dead_code)]
pub fn test09() {

    // [T,n] T 表示类型, n 表示长度
    let arr1: [i8; 3] = [100, 23, 89];

    // 类型自动推断
    let arr2 = [99, 33, 44, 88];

    // 表示i8类型的4个元素的类型
    let arr3 = [1i8; 4];

    // arr1=[100, 23, 89]
    println!("arr1={:?}", arr1);

    // arr2=[99, 33, 44, 88]
    println!("arr2={:?}, arr2[1]={}", arr2, arr2[1]);

    // arr3=[1, 1, 1, 1]
    println!("arr3={:?}", arr3);
}

#[allow(dead_code)]
pub fn test08() {
    let tup1 = (1, 0.1, true);
    println!("{}, {}, {}", tup1.0, tup1.1, tup1.2);

    // {:.4} 这里是保留4位小数，4舍5入
    let tup2 = (false, (1, 3.89434, 'C'));
    println!("{:.4}, {}", tup2.1.1, tup2.1.2);

    // {：？}
    let (x, y) = tup2;
    println!("{}, {:?}", x, y);
}

#[allow(dead_code)]
pub fn test07() {
    // 左闭右开区间，(1, 5] = 1,2,3,4，不包含右值
    for i in 1..5 {
        println!("{}", i);
    }
    println!();

    // 反转序列
    for i in (1..5).rev() {
        println!("{}", i);
    }
    println!();
    let sum: i32 = (1..5).sum();
    println!("sum={}", sum);
}

#[allow(dead_code)]
pub fn test06() {
    // 每个字符占4个字节
    let a = 'A';
    println!("a={}", a);
}

#[allow(dead_code)]
pub fn test05() {
    let b1 = true;
    let b2 = false;
    let b3 = b1 && b2;
    let b4 = b1 && b2;
    println!("{}", b1);
    println!("{}", b2);
    println!("{}", b3);
    println!("{}", b4);
}

#[allow(dead_code)]
pub fn test04() {
    let int1: u8 = 1;
    let int2 = 1;
    let int3 = 11u16;

    println!("{}", int1);
    println!("{}", int2);
    println!("{}", int3);

    // 默认 float64
    // f32 精度6位
    // f64 精度15位
    let f1 = 1.01113;
    let f2: f32 = 2.044455698; // 后6位

    let f3 = 32_000.55555_001;
    println!("{}", f1);
    println!("{}", f2);
    println!("{}", f3);
}

#[allow(dead_code)]
pub fn test03() {
    const MAX_NUM: i32 = 315;

    println!("MAX_NUM: {}", MAX_NUM);
}

#[allow(dead_code)]
pub fn test02() {
    let x = 10;
    println!("x: {}", x);

    // 变量遮蔽
    let x = 20;
    println!("x: {}", x);

    let x = "hello rust";
    println!("x: {}", x);
}

#[allow(dead_code)]
pub fn test01() {
    let x = 10;
    // 默认是不可变的
    // x = 11;

    println!("x: {}", x);
}
